package com.cskaoyan.gateway.controller.pay;

import com.cskaoyan.gateway.form.pay.PayForm;
import com.mall.commons.result.ResponseData;
import com.mall.commons.result.ResponseUtil;
import com.mall.pay.PayCoreService;
import com.mall.pay.constants.PayReturnCodeEnum;
import com.mall.pay.dto.PaymentRequest;
import com.mall.pay.dto.alipay.AlipayQueryRetResponse;
import com.mall.pay.dto.alipay.AlipaymentResponse;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @author hanshuai
 * @version 1.0
 * @description @TODO
 * @date 2021/12/16 16:13
 */
@RestController
@RequestMapping("cashier")
public class PayController {
    @Reference(timeout = 3000, retries = 2, check = false)
    PayCoreService payCoreService;

    ////
//    /**
//     * 处理生成二维码
//     *
//     * @param payForm
//     * @param request
//     * @return
//     */
    @RequestMapping("/pay")
    public ResponseData preCreate(@RequestBody PayForm payForm, HttpServletRequest request) {
        PaymentRequest paymentRequest = new PaymentRequest();
        paymentRequest.setPayChannel(payForm.getPayType());
        //先让userId不为null
        paymentRequest.setUserId(0L);
        paymentRequest.setSubject(payForm.getInfo());
        paymentRequest.setOrderFee(payForm.getMoney());
        paymentRequest.setTotalFee(payForm.getMoney());
        paymentRequest.setTradeNo(payForm.getOrderId());
        paymentRequest.setSpbillCreateIp(request.getRemoteAddr());
        //新增一个username变量
        paymentRequest.setUsername(payForm.getNickName());

        AlipaymentResponse alipaymentResponse = payCoreService.aliPay(paymentRequest);

        if (!alipaymentResponse.getCode().equals(PayReturnCodeEnum.SUCCESS.getCode())) {
            return new ResponseUtil().setErrorMsg(alipaymentResponse.getMsg());
        }
        alipaymentResponse.setQrCode("http://localhost:8080/image/" + alipaymentResponse.getQrCode());
        return new ResponseUtil().setData(alipaymentResponse.getQrCode());
    }

    @RequestMapping("queryStatus")
    public ResponseData queryStatus(String orderId, HttpServletRequest request) {
        PaymentRequest paymentRequest = new PaymentRequest();
        paymentRequest.setTradeNo(orderId);
        paymentRequest.setSubject("1");
        paymentRequest.setUsername("1");
        paymentRequest.setUserId(0L);
        paymentRequest.setSpbillCreateIp(request.getRemoteAddr());
        AlipayQueryRetResponse alipayQueryRetResponse = payCoreService.queryAlipayRet(paymentRequest);
        if (!alipayQueryRetResponse.getCode().equals(PayReturnCodeEnum.SUCCESS.getCode())) {
            return new ResponseUtil().setErrorMsg(alipayQueryRetResponse.getMsg());
        }
        return new ResponseUtil().setData(null);
    }

}
